module game {
    export class CoreView extends BaseEuiView {
        public btn_home: eui.Image;
        public txt_score: eui.Label;
        public effect: eui.Image;
        public btn_hammar: eui.Image;
        public btn_reset: eui.Image;
        public txt_hammar: eui.Label;
        public txt_reset: eui.Label;
        public bg1: eui.Rect;
        public bg0: eui.Rect;
        public com: eui.Component;
        
        public txt_tipreset: eui.Label;
        public txt_tipok: eui.Group;
        public txt_no:eui.Label;
        public txt_tip3: eui.Label;

        private itemList: CandyMap;
        private id: number = 0;

        public txt_tipok0: eui.Group;
        public txt_no1:eui.Label;
        public bg2: eui.Rect;

        public txt_tip_1: eui.Group;
        public txt_tip_1_1:eui.Label;
        public bg_tip_1: eui.Rect;
        
        public txt_tip: eui.Group;
        public txt_tip15:eui.Label;
        public bg15: eui.Rect;

        public constructor($m: BaseController, $parent: eui.Group) {
            super($m, $parent);
            this.skinName = "resource/skin/Core.exml";
        }

        public initUI(): void {
            super.initUI();

            const self = this;
            this.btn_home.addEventListener(egret.TouchEvent.TOUCH_TAP, this.backHomeHandler, this);
            this.btn_hammar.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickHandler, this);
            this.btn_reset.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickHandler, this);

            this.skin.width = egret.MainContext.instance.stage.stageWidth;
            this.skin.height = egret.MainContext.instance.stage.stageHeight;
            this.bg1.width = this.skin.width;
            this.bg1.height = this.skin.height;

            this.txt_tipok0.touchEnabled = false;
            this.txt_no1.touchEnabled = false;
            this.bg2.touchEnabled = false;

            this.txt_tip_1.touchEnabled = false;
            this.txt_tip_1_1.touchEnabled = false;
            this.bg_tip_1.touchEnabled = false;
            
        }

        private onClickHandler(e: egret.TouchEvent) {
            let hammar_big = function (target) {
                egret.Tween.removeTweens(target);
                egret.Tween.get(target).to({ scaleX: 1.2, scaleY: 1.2 }, 100).call(hammar_small, this, [target]);
            }
            let hammar_small = function (target) {
                egret.Tween.removeTweens(target);
                egret.Tween.get(target).to({ scaleX: 0.9, scaleY: 0.9 }, 100).call(hammar_big, this, [target]);
            }
            switch (e.target) {
                case this.btn_hammar:
                    //无道具了
                    if (gameData.getAllHammar() <= 0)
                    {
                        //显示奖励分享面板
                        App.ViewManager.open(ViewConst.Gift,1);
                        return;
                    } 
                    //没有次数了
                    if (gameData.hammarCount <= 0){
                        this.txt_tipok.alpha = 0;
                        this.txt_tipok.y = 606;
                        this.txt_no.text = "当前次数已用完";
                        this.bg0.width = 460;
                        this.showTips("当前次数已用完", this.txt_tipok, 1500);

                        
                        return;
                    } 
                    gameData.isHammaring = !gameData.isHammaring;
                    this.btn_hammar.scaleX = 1;
                    this.btn_hammar.scaleY = 1;
                    if (gameData.isHammaring) {
                        this.showTips("请点击需要去掉的方块", this.txt_tipreset);
                        hammar_big(this.btn_hammar);
                    } else {
                        egret.Tween.removeTweens(this.btn_hammar);
                    }


                    break;
                case this.btn_reset:
                    //无道具了
                    if (gameData.getAllReset() <= 0)
                    {
                        App.ViewManager.open(ViewConst.Gift,0);
                        return;
                    } 
                    //没有次数了
                    if (gameData.resetCount <= 0) {
                        this.txt_tipok.alpha = 0;
                        this.txt_tipok.y = 606;
                        this.txt_no.text = "当前次数已用完";
                        this.bg0.width = 460;
                        this.showTips("当前次数已用完", this.txt_tipok, 1500);
                        
                        return;
                    }

                    if(gameData.preMapDic.length <= 0) return;
                    gameData.removeResetItem();
                    gameData.resetCount -= 1;
                    this.resetMap();
                    break;
            }
        }

        private tween_Scale() {
            egret.Tween.removeTweens(this.btn_hammar);
            egret.Tween.get(this.btn_hammar).to({ scale: 1.5 }, 50).call(this.tween_right, this);
        }

        private tween_right() {
            egret.Tween.removeTweens(this.btn_hammar);
            egret.Tween.get(this.btn_hammar).to({ scale: 1 }, 50).call(this.tween_Scale, this);
        }

        public open(...param: any[]): void {
            super.open(...param);
            this.effect.alpha = 0;
            this.randomCandy();


            this.txt_score.text = gameData.historyScore + "";
            this.txt_reset.text = gameData.getAllReset() + "";
            this.txt_hammar.text = gameData.getAllHammar() + "";

            this.txt_tip.alpha = 0;
            this.txt_tipreset.alpha = 0;
            this.txt_tipok.alpha = 0;
            this.txt_tip3.alpha = 0;

            this.isResetting = false;

            this.txt_tipok.y = 222;
            this.txt_tip.y = 606;
            this.showTips("点击相邻的数字即可合并，目标合到15!", this.txt_tip, 3000);


            App.TimerManager.doTimer(10 * 1000, 0, this.randowTips, this);

        }

        public removeFromParent() {
            super.removeFromParent();
            App.TimerManager.remove(this.randowTips, this);
        }

        private randowTips() {
            let count = Math.floor(Math.random() * gameData.tips.length);
            this.txt_tip3.text = gameData.tips[count];
            this.showTips("randow", this.txt_tip3, 3000);
        }


        private backHomeHandler() {
            //clearAll
            App.ViewManager.close(ViewConst.Core);
            App.ViewManager.open(ViewConst.Home);
        }

        public rows: number = 5;
        public cols: number = 5;
        private randomCandy() {
            var arr = [];
            this.clearCandy();
            this.itemList = {};

            this.isShowMax = false;
            let map = gameData.mapDic;
            for (var i = 0, iLen = this.rows; i < iLen; i++) {
                for (var j = 0, jLen = this.cols; j < jLen; j++) {
                    let key = i + "_" + j;
                    let type = map[key];
                    this.createCandyMC(i, j,type);
                }
            }

            this.checkAll(true);

        }

        public showAllEdge(list: CandyMap = null): void {
            if (!list) list = this.itemList;
            for (let key in list) {
                let item = list[key];
                let itemType = item.voCandy.type;

                let showEdge = (row, col, type) => {
                    let targetItem = this.getItem(row, col, list);

                    if (targetItem && itemType == targetItem.voCandy.type) {
                        item.setEdge(type);
                    }
                }

                showEdge(item.voCandy.row - 1, item.voCandy.col, EdgeType.Left);
                showEdge(item.voCandy.row + 1, item.voCandy.col, EdgeType.Right);
                showEdge(item.voCandy.row, item.voCandy.col - 1, EdgeType.Up);
                showEdge(item.voCandy.row, item.voCandy.col + 1, EdgeType.Down);

            }
        }
        public hideAllEdge(list: CandyMap = null): void {
            if (!list) list = this.itemList;
            for (let key in list) {
                let item = list[key];
                item.reset();
            }
        }
        public getItem(row: number, col: number, list: CandyMap = null): McCandy {
            if (!list) list = this.itemList;
            return list[row + "_" + col]
        }
        private createMap(list: McCandy[]): CandyMap {
            let map: CandyMap = {};

            list.forEach((ele) => { map[ele.voCandy.row + "_" + ele.voCandy.col] = ele; })
            return map;

        }
        private noMoveCandy(list: McCandy[]): CandyMap {
            let map: CandyMap = {};
            list.forEach((ele) => {
                let item = map[ele.voCandy.row];
                if (item) {
                    if (item.voCandy.col < ele.voCandy.col) {
                        map[ele.voCandy.row] = ele;
                    }
                } else {
                    map[ele.voCandy.row] = ele;
                }

            });
            let map2: CandyMap = {};
            let list2 = this.itemList;
            for (let key in list2) {
                let item = list2[key];
                let moveItem = map[item.voCandy.row];
                if (moveItem && moveItem.voCandy.col >= item.voCandy.col) {
                    continue;
                }
                map2[item.voCandy.row + "_" + item.voCandy.col] = item
            }
            // list = list.filter((ele) => { return true });
            return map2;
        }


        private clearCandy() {
            let dic = this.itemList;
            for (let key in dic) {
                let mc = dic[key];
                if (mc) {
                    mc.dispose();
                    mc = null;
                }
            }
            dic = {};
        }
        private getMaxLen(mcCandy: McCandy, candyList: any[]) {
            let maxRow = 0;
            let maxCol = 0;
            for (let item of candyList) {
                maxRow = Math.max(maxRow, Math.abs(mcCandy.voCandy.row - item.voCandy.row));
                maxCol = Math.max(maxCol, Math.abs(mcCandy.voCandy.col - item.voCandy.col));
            }
            return Math.max(maxRow, maxCol);
        }
        private getLenList(mcCandy: McCandy, candyList: any[]) {
            let maxRow = 0;
            let maxCol = 0;
            let mapLen:{[key:number]:number}  ={}
            candyList.forEach((ele)=>{mapLen[this.genLen(mcCandy,ele)] = 1});
            let LenList = []
            for (let key in mapLen) {
                LenList.push(key);

                
            }
            return LenList;
        }
        private genLen(mcCandy: McCandy,mcCandy2:McCandy):number{
                let maxRow = Math.abs(mcCandy.voCandy.row - mcCandy2.voCandy.row);
                let maxCol = Math.abs(mcCandy.voCandy.col - mcCandy2.voCandy.col);
                return maxRow*maxRow + maxCol*maxCol;
        }

        //清理candy
        public removeCandy(mcCandy: McCandy) {
            if (!mcCandy) return;

            let checklist = this.checkRound(mcCandy);
            let count = checklist.length;

            //有消掉的
            if (count > 1) {
                //把一样的消掉
                // let timeGap = 1000;
                let timeGap = 50;
                let maxGap = this.getMaxLen(mcCandy, checklist);
                for (let i = 1; i <= maxGap; i++) {
                    let targetGap = maxGap -i +1;
                    egret.setTimeout(() => {
                        let disList = checklist.filter((ele) => {
                            return Math.abs(ele.voCandy.row - mcCandy.voCandy.row) == targetGap || Math.abs(ele.voCandy.col - mcCandy.voCandy.col) == targetGap;
                        });
                

                        checklist = checklist.filter((ele) => {
                            return !(Math.abs(ele.voCandy.row - mcCandy.voCandy.row) == targetGap || Math.abs(ele.voCandy.col - mcCandy.voCandy.col) == targetGap);
                        });
                        disList.forEach((ele) => {
                            let key = ele.voCandy.row + "_" + ele.voCandy.col;
                            delete this.itemList[key];
                            delete gameData.mapDic[key];
                            ele.dispose();
                            // ele.alpha = 0.5;
                            // if(ele.parent)
                            //     ele.parent.addChildAt(ele,0);
                            // ele.ani();
                            // egret.setTimeout((item) => { item.dispose() }, this, timeGap/2,ele);
                            
                        })
                        let map = this.createMap(checklist)
                        this.hideAllEdge(map);
                        this.showAllEdge(map);
                    }, this, i * timeGap);
                    this.hideAllEdge();// 清理全部
                    this.showAllEdge(this.createMap(checklist));//显示 被消除
                    this.showAllEdge(this.noMoveCandy(checklist));//显示 不动的
                }

                // let timeGap = 1000;
                // let timeGap = 50;
                // let maxlenList = this.getLenList(mcCandy, checklist);
                // let maxGap = maxlenList.length;
                // for (let i = 0,len=maxGap; i < len; i++) {
                //     let targetLen = maxlenList[len-i-1];
                //     egret.setTimeout(() => {
                //         let disList = checklist.filter((ele) => {
                //             return targetLen == this.genLen(ele,mcCandy);
                //         });
                        
                //         checklist = checklist.filter((ele) => {
                //             return !( targetLen == this.genLen(ele,mcCandy));
                //         });
                //         disList.forEach((ele) => {
                //             let key = ele.voCandy.row + "_" + ele.voCandy.col;
                //             delete this.itemList[key];
                //             delete gameData.mapDic[key];
                //             // ele.alpha = 0.5;
                //             // ele.parent.addChildAt(ele,0);
                //             // egret.setTimeout(() => { ele.dispose() }, this, timeGap);
                //             ele.dispose();
                //         })
                //         let map = this.createMap(checklist)
                //         this.hideAllEdge(map);
                //         this.showAllEdge(map);
                        
                //     }, this, i * timeGap);
                //     this.hideAllEdge();// 清理全部
                //     this.showAllEdge(this.createMap(checklist));//显示 被消除
                //     this.showAllEdge(this.noMoveCandy(checklist));//显示 不动的
                // }
                

                egret.setTimeout(() => {
                    //点击mc+1
                    // this.hideAllEdge();
                    let type = mcCandy.voCandy.type + 1;
                    //最大数字改变
                    let isRemoveMin = false;
                    if (type > gameData.curmax) {
                        gameData.curmax = type;
                        //每次更改都判断存储
                        CoreView.storeResult(gameData.curmax);
                        //道具礼包
                        App.ViewManager.open(ViewConst.Gift);
                        //消除掉全場最小數
                        this.checkMin();
                        this.removeMin();
                        isRemoveMin = true;

                        // if (type > gameData.targetNumber) {
                        //     //大于15个弹提示
                        //     App.ViewManager.open(ViewConst.GameGoal);
                        // }
                    }
                    mcCandy.voCandy.type = type;
                    mcCandy.setCandyValue(mcCandy.voCandy)
                    //check
                    this.checkColor();

                    let time = 200;
                    if (isRemoveMin) time = 700;
                    App.TimerManager.doTimer(time, 1, this.restart, this);
                    App.TimerManager.doTimer(time + 200, 1, this.replenish, this);
                }, this, timeGap * (maxGap))


                // for (var i = 0; i < count; i++) {
                //     var mc: McCandy = checklist[i];
                //     if (mc && mc.voCandy.id != mcCandy.voCandy.id) {
                //         let key = mc.voCandy.row + "_" + mc.voCandy.col;
                //         delete this.itemList[key];
                //         delete gameData.mapDic[key]
                //         mc.dispose();
                //     }
                // }

                // //点击mc+1
                // let type = mcCandy.voCandy.type + 1;
                // //最大数字改变
                // if (type > gameData.curmax) {
                //     gameData.curmax = type;
                //     //道具礼包
                //     App.ViewManager.open(ViewConst.Gift);

                //     if (type > gameData.targetNumber) {
                //         //大于15个弹提示
                //         App.ViewManager.open(ViewConst.GameGoal);
                //     }
                // }
                // mcCandy.voCandy.type = type;
                // mcCandy.setCandyValue(mcCandy.voCandy)
                // //check
                // this.checkColor();

                // App.TimerManager.doTimer(200, 1, this.restart, this);
                // App.TimerManager.doTimer(400, 1, this.replenish, this);
            } else {
                //没有可以和的
            }
        }

        private removeMin() {
            var row = 5;
            var col = 5;
            let min = gameData.min;
            for (var j = 0; j < row; j++) {
                for (var k = 0; k < col; k++) {
                    let key = j + "_" + k;
                    let mc = this.itemList[key];
                    if (mc && mc.voCandy.type == min) {
                        mc.dispose();
                        delete this.itemList[key];
                        delete gameData.mapDic[key]
                    }
                }
            }
        }

        private restart() {
            var row = 5;
            var col = 5;
            for (var j = 0; j < row; j++) {
                for (var k = col - 1; k >= 0; k--) {
                    let mc: McCandy = this.itemList[j + "_" + k];
                    if (mc) {
                        while (this.checkDownMc(mc, mc.voCandy.col + 1) == true) {
                            mc.voCandy.y += 118;
                            egret.Tween.removeTweens(mc);
                            egret.Tween.get(mc).to({ y: mc.voCandy.y }, 200);
                        }
                    }
                }
            }
        }

        //补全
        private replenish() {
            var row = 5;
            var col = 5;
            let max = gameData.curmax;
            for (var j = 0; j < row; j++) {
                for (var k = 0; k < col; k++) {
                    let mc = this.itemList[j + "_" + k];
                    if (!mc) {
                        this.createCandyMC(j, k);
                    }
                }
            }

            this.checkAll();
        }


        private createCandyMC(i, j, type?) {
            let mcCandy = new McCandy();
            let max = gameData.curmax;
            let vo;
            vo = new CandyVO();
            vo.id = this.id;
            vo.width = 110;
            vo.height = 110;
            vo.row = i;
            vo.col = j;
            vo.type = type ? type : this.getType(i, j);//Math.floor(Math.random()*max)+1;
            vo.x = i * 118; //123 + i * 118;
            vo.y = j * 118;//433 + j * 118;

            mcCandy.x = vo.x;
            mcCandy.y = vo.y;

            mcCandy.init(vo);

            this.com.addChild(mcCandy);

            this.id++;
            this.itemList[i + "_" + j] = mcCandy;
            gameData.mapDic[i + "_" + j] = mcCandy.voCandy.type;

        }

        private isShowMax: boolean = false;
        private getType(i, j) {
            let type = 0;
            if (j == 4 && this.isShowMax == false) {
                //最后一行肯定会出现1个最大数字
                var key = 0
                if (i == 4) {
                    //最后一个还没出现
                    type = gameData.curmax;
                } else {
                    //最后一行生成公式
                    if (Math.random() > 0.7) {
                        type = gameData.curmax;
                    } else {
                        let min = 1;
                        let max = 6;
                        if (gameData.curmax == 6) {
                            min = 1;
                            max = 5;
                        } else {
                            min = Math.floor((gameData.curmax - 5) / 2)
                            max = min + (gameData.curmax - 5) % 2 + 5
                        }

                        let count = max - min + 1;
                        let probability: number[] = gameData.config[count];

                        let random = Math.random();
                        let pre: number = 0
                        for (key = 0; key < count; key++) {
                            let percet = probability[key];
                            let next = pre + percet;
                            if (pre <= random && random <= next) {
                                break;
                            }
                            pre += percet;
                        }

                        type = min + key;

                    }
                }

            } else {
                //其他行数

                let min = 1;
                let max = 6;
                if (gameData.curmax == 6) {
                    min = 1;
                    max = 5;
                } else {
                    min = Math.floor((gameData.curmax - 5) / 2)
                    max = min + (gameData.curmax - 5) % 2 + 5
                }

                let count = max - min + 1;
                let probability: number[] = gameData.config[count];

                let random = Math.random();
                let pre: number = 0
                for (key = 0; key < count; key++) {
                    let percet = probability[key];
                    let next = pre + percet;

                    if (pre <= random && random <= next) {
                        break;
                    }
                    pre += percet;
                }

                type = min + key;

            }

            if (type == gameData.curmax) {
                this.isShowMax = true;
            }

            return type;
        }


        /**********check*************** */
        private checkList: McCandy[];
        private checkRound(mc) {
            this.checkList = [];
            this.check(mc);
            return this.checkList;
        }

        private checkMin() {
            let row = 5;
            let col = 5;
            gameData.min = 1000;
            for (let j = 0; j < row; j++) {
                for (let k = 0; k < col; k++) {
                    let mc = this.itemList[j + "_" + k];
                    if (mc && gameData.min > mc.voCandy.type) {
                        gameData.min = mc.voCandy.type;
                    }
                }
            }
        }

        private check(mcCandy: McCandy) {
            if (this.checkList.indexOf(mcCandy) > -1) return;
            this.checkList.push(mcCandy);
            let arr = [];
            let type = mcCandy.voCandy.type;

            let row = mcCandy.voCandy.row;
            let preRow = row - 1;
            let backRow = row + 1;
            let col = mcCandy.voCandy.col;
            let preCol = col - 1;
            let backCol = col + 1;


            let left: McCandy = preRow > -1 ? this.itemList[preRow + "_" + col] : null;
            let right: McCandy = backRow > -1 ? this.itemList[backRow + "_" + col] : null;
            let down: McCandy = backCol > -1 ? this.itemList[row + "_" + backCol] : null;
            let up: McCandy = preCol > -1 ? this.itemList[row + "_" + preCol] : null;

            if (left && left.voCandy.type == type) {
                arr.push(left);
            }

            if (right && right.voCandy.type == type) {
                arr.push(right);
            }
            if (up && up.voCandy.type == type) {
                arr.push(up);
            }
            if (down && down.voCandy.type == type) {
                arr.push(down);
            }

            let count = arr.length;
            for (let i = 0; i < count; i++) {
                this.check(arr[i]);
            }
        }


        private checkDownMc(mc: McCandy, col) {
            if (col > 4) return false;
            var downMC = this.itemList[mc.voCandy.row + "_" + col];
            if (!downMC) {
                let key = mc.voCandy.row + "_" + mc.voCandy.col;
                delete this.itemList[key];
                delete gameData.mapDic[key];
                mc.voCandy.col = col;
                key = mc.voCandy.row + "_" + mc.voCandy.col;
                this.itemList[key] = mc;
                gameData.mapDic[key] = mc.voCandy.type;
                return true;
            }
            return false;
        }

        private checkColor() {
            var row = 5;
            var col = 5;
            for (var j = 0; j < row; j++) {
                for (var k = 0; k < col; k++) {
                    let mc: McCandy = this.itemList[j + "_" + k];
                    if (mc) {
                        mc.setCandyValue(mc.voCandy);
                    }
                }
            }
        }

        private _canRemoveDic: { [index: number]: McCandy };
        private checkAll(isRedo = false) {
            this.effect.alpha = 0;
            if (this.itemList) {
                this._canRemoveDic = {};
                let count = 0;
                for (let key in this.itemList) {
                    let mcCandy = this.itemList[key];
                    if (mcCandy) {
                        // mcCandy.reset();
                        var checklist = this.checkRound(mcCandy);
                        if (checklist.length >= 2) {
                            count++;
                            this._canRemoveDic[mcCandy.voCandy.id] = mcCandy;
                        }
                    }
                }

                if (count < 1) {
                    //没有道具了
                    for (let key in this.itemList) {
                        let mcCandy = this.itemList[key];
                        if (mcCandy) {
                            mcCandy.gameOver();
                        }
                    }

                    if (isRedo) {
                        App.TimerManager.doTimer(3500, 1, this.randomCandy, this);
                    } else {
                        //挂了
                        App.TimerManager.doTimer(1000, 1, this.gameOver, this);
                    }
                } else {
                    //check max
                    for (let key in this.itemList) {
                        let mcCandy = this.itemList[key];
                        if (mcCandy && mcCandy.voCandy.type == gameData.curmax) {
                            var checklist = this.checkRound(mcCandy);
                            if (checklist.length >= 2) {
                                this.showMaxEffect();
                                break;
                            }
                        }
                    }
                }
            }
            this.hideAllEdge();
            this.showAllEdge();

            gameData.saveWXData();
        }

        private showMaxEffect() {
            this.effect.alpha = 1;
        }

        private gameOver() {
            if (gameData.reviveCount == 0) {
                //存储结果
                CoreView.storeResult(gameData.curmax);
                //显示面板
                App.ViewManager.open(ViewConst.GameResult);

            } else {
                App.ViewManager.open(ViewConst.GameOver);
            }
        }

        public showTipsForShare2(str: string, time) {
            this.txt_tip_1_1.text = str;
            this.txt_tip_1_1.visible = true;
            this.txt_tip_1_1.alpha = 1;

            this.txt_tip_1.visible = true;
            let callfun2 = function () {
                this.txt_tip_1.alpha = 0;
            }
            egret.Tween.removeTweens(this.txt_tip_1);
            //egret.Tween.get(this.txt_tip_1).wait(time).call(callfun2, this);
            egret.setTimeout(callfun2,this,time);
        }

        public showTipsForShare(str: string, time) {
            this.txt_no1.text = str;
            this.txt_no1.visible = true;
            this.txt_no1.alpha = 1;

            this.bg2.visible = true;
            this.bg2.alpha = 1;

            this.txt_tipok0.y = 606;
            this.txt_tipok0.visible = true;
            this.txt_tipok0.alpha = 1;

            let callfun1 = function () {
                this.txt_tipok0.alpha = 0;
            }
            egret.Tween.removeTweens(this.txt_tipok0);
            //egret.Tween.get(this.txt_tipok0).wait(time).call(callfun1, this);
            egret.setTimeout(callfun1,this,time);
        }

        public showTips(str: string, label, time = 2000) {
            //label.text = str;
            let callfun = function () {
                label.alpha = 0;
                if (label == this.txt_tipok || label == this.txt_tip) {
                    label.y = 220;
                }
            }
            egret.Tween.removeTweens(label);
            egret.Tween.get(label).to({ alpha: 1 }, 800).wait(time).call(callfun, this);
        }

        //保存上一版本的地图信息
        public saveVersion() {
            let premap = {};
            for (var i = 0; i < 5; i++) {
                for (var j = 0; j < 5; j++) {
                    let key = i + "_" + j;
                    if (this.itemList[key]) {
                        premap[key] = this.itemList[key].voCandy.type;
                    }
                }
            }
            gameData.preMapDic.unshift(premap);
        }

        //敲碎一块
        public hammarCandy(mc: McCandy) {
            this.btn_reset.scaleX = 1;
            this.btn_reset.scaleY = 1;
            egret.Tween.removeTweens(this.btn_hammar);
            this.txt_hammar.text = gameData.getAllHammar() + "";

            let key = mc.voCandy.row + "_" + mc.voCandy.col;
            delete this.itemList[key];
            delete gameData.mapDic[key];
            mc.dispose();

            App.TimerManager.doTimer(200, 1, this.restart, this);
            App.TimerManager.doTimer(400, 1, this.replenish, this);
        }

        //重置地图

        public isResetting:boolean = false;
        public resetMap() {
            if(this.isResetting) return;
            let pre = gameData.preMapDic.shift();
            if (!pre) return;
            this.btn_reset.scaleX = 1;
            this.btn_reset.scaleY = 1;
            egret.Tween.removeTweens(this.btn_reset);
            this.txt_reset.text = gameData.getAllReset() + "";

            this.isResetting = true;
            this.clearCandy();
            this.itemList = {};
            this.isShowMax = false;

            for (var i = 0; i < 5; i++) {
                for (var j = 0; j < 5; j++) {
                    let key = i + "_" + j;
                    let type = pre[key];
                    this.createCandyMC(i, j, type);
                }
            }

            this.checkAll();

            this.txt_tipok.alpha = 0;
            this.txt_tipok.y = 606;
            this.txt_no.text = "撤消成功!";
            this.bg0.width = 288;
            this.showTips("撤消成功", this.txt_tipok, 1500);

            this.isResetting = false;

        }

        //结果处理
        public static async storeResult(new_score) {
            console.log("存储结果");
            var week = ToolUtil.getMonDate();
            var week_string = week.getFullYear() + "-" + week.getMonth() + "-" + week.getDate();
            var score = platform.getStorageSync("userscore");
            var week_score = platform.getStorageSync("weekscore_" + week_string);

            if (!score) {
                score = 0;
            }

            if (!week_score) {
                week_score = 0;
            }

            var best = score;
            var best_week = week_score;
            var attack: number = new_score;
            //判断是否为最好成绩
            if (attack > score) {
                platform.setStorageSync("userscore", attack);
                best = attack;
            }
            // 判断是否为本周最好成绩
            if (attack > week_score) {
                platform.setStorageSync("weekscore_" + week_string, attack);
                best_week = attack;
            }

            let result = await platform.setUserCloudStorage([{ key: "userscore", value: best.toString() },
            { key: "weekscore_" + week_string, value: best_week + "" },
            {
                key: "gamecenterscore",
                value: JSON.stringify({
                    "wxgame": {
                        "score": best,
                        "update_time": Math.floor(new Date().getTime() / 1000)
                    }
                })
            }]);
        }
    }

    interface CandyMap {
        [index: string]: McCandy;
    }
}